<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Airmail
 *
 * @author ketu
 */
class Zhishidi_Express_Model_Carrier_Express extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface {

    protected $_code = 'zhishidi_express';

    public function __construct() {
        parent::__construct();
    }

    public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
        $freeBoxes = 0;
        $found = false;
        $virtualTotal = 0;
        foreach ($request->getAllItems() as $item) {
            if ($item->getFreeShipping() && $item->getProductType() != Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL
                    && $item->getProductType() != 'downloadable') {
                $freeBoxes+=$item->getQty();
            }
            if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL ||
                    $item->getProductType() == 'downloadable') {
                $virtualTotal+= $item->getBaseRowTotal();
                $found = true;
            }
        }
        if ($found && $this->getConfigFlag('remove_virtual')) {
            $request->setPackageValue($request->getPackageValue() - $virtualTotal);
        }
        $this->setFreeBoxes($freeBoxes);


        $result = Mage::getModel('shipping/rate_result');

        $postCode = $request->getDestPostcode();
        $countryId = $request->getDestCountryId();

        $regionId = $request->getDestRegionId();
        $regionCode = $request->getDestRegionCode();
        if ($regionId) {
            $region = Mage::getModel('directory/region')->load($regionId);
            $regionCode = $region->getName();
        }

        $packageWeight = $request->getPackageWeight();

        $carriers = Mage::getModel('zhishidi_express/express')->getExpress($packageWeight, $countryId, $regionCode);
        # var_dump($carriers);

        if ($this->getConfigData('enable_free_shipping_threshold') && $this->getConfigData('free_shipping_threshold') > 0 && $request->getPackageValue() > $this->getConfigData('free_shipping_threshold')) {
            $costArray = array();
            $minShippingCost = 0;
            foreach ($carriers as $carrierCode => $carrier) {
                $costArray[] = $carrier['cost'];
            }
            $minShippingCost = min($costArray);
            foreach ($carriers as $carrierCode => $carrier) {
                $cost = $carrier['cost'] - $minShippingCost < 0 ? 0 : $carrier['cost'] - $minShippingCost;
                $carriers[$carrierCode]['cost'] = $cost;
            }
        }



        foreach ($carriers as $carrierCode => $carrier) {
            if (!empty($carrier) && $carrier['cost'] >= 0) {
                $method = Mage::getModel('shipping/rate_result_method');
                $method->setCarrier($this->_code);
                $method->setCarrierTitle($this->getConfigData('title'));
                $method->setMethod($carrierCode);
                $method->setMethodTitle($carrier['title']);

                $shippingPrice = $this->getFinalPriceWithHandlingFee($carrier['cost']);
                $method->setCost($shippingPrice);

                $method->setPrice($shippingPrice);

                $result->append($method);
            }
        }
        return $result;
    }

    public function getAllowedMethods() {
        return array($this->_code => $this->getConfigData('name'));
    }

}